import axios from 'axios'
import Vue from 'vue'

//axios.defaults.baseURL = 'http://192.168.3.9/hard/'  //
//axios.defaults.baseURL = 'http://localhost:9090/'  //笔记本电脑
axios.defaults.baseURL = 'http://localhost/hard/'
//--获取token------
export const getToken = (url,username,password) => {
  return  axios.get(url +`api/Login/GetJwtToken`, {
        params: {
            username: username,
            password: password
        }
    }).then((res) => {
        window.sessionStorage.setItem("token", res.data.data.token.token);
        return 0;
    }).catch(()=>{
        //console.log("网络连接失败==============");
      return 1;
    });
};
//-获取瓶子信息---------<!--  -->
export const getItem = (url,code) => {
    return Vue.prototype.$httpx.get(url +`api/PhyscialMachine/GetRecycleItemList`, {
        params: {
            name: '',
            code: code,
            type: "",
            pageSize: 10,
            pageIndex: 1,
        }
    }).then((res) => {
        return res.data.response;
    });

};


//-------<!--  -->
//-获取广告--------
export const getAdv = (url,code) => {
    return Vue.prototype.$httpx.get(url +`api/PhyscialMachine/GetAdvList`, {
        params: {
            machineCode: code
        }
    }).then((res) => {
        return res.data;
    });

};

//---------------------
export const initList = () => {
    return axios.get(`/GetURL`).then((res) => {
        return res.data;
    });
};


//---------------------
export const checkState = () => {
    return axios.get(`/checkState`).then((res) => {
        return res.data;
    });
};

//判断有没有瓶子信息
export const ifHasCode = (url,code) => {
    return Vue.prototype.$httpx.get(url +`api/PhyscialMachine/GetRecycleItemList`, {
        params: {
            name: '',
            code: code,
            type: "",
            pageSize: 10,
            pageIndex: 1,
        }
    }).then((res) => {

        return res.data.response;
    }).catch((res) => {
        return 1;
    });
};


//上传图片
export const uploadImage = (url,filepath) => {
    let imgFile="c:\\1.png";
    let formData=new FormData();
    formData.append('file',imgFile);
return Vue.prototype.$httpx.post(url+'api/CosSts/UpLoadFile',
    formData,
    {
    method: 'post',
    headers: {'Content-Type': 'multipart/form-data'}
    }).then((res) => {
        return res.data;
    });
};


//退瓶操作
export const backBottle = () => {
    return axios.get(`/backBottle`).then((res) => {
        return res.data;
    });
};


//开始回收
// export const startRecycle = (bottle) => {
//     //console.log(bottle);
//     //console.log("===========");
//     return axios.post(`/PostRecycleItemCode`, bottle).then((res) => {
//         return res.data;
//     });
// };




//瓶子是否到位
export const onPlace = () => {
    return axios.get(`/onPlace`).then((res) => {
        return res.data;
    });
};

//瓶子是否被取走,0被取走，1，未被取走
export const onRemove = () => {
    return axios.get(`/remove`).then((res) => {
        return res.data;
    });
};


//读取硬件设备的状态
export const getHardwardStatue = () => {
    return axios.get(`/GetStatue`).then((res) => {
        return res.data;
    });
};

//开门
export const openDoor = () => {
    return axios.get(`/open`).then((res) => {
        return res.data;
    });
};


//判断是否在左转状态
export const onLeft = () => {
      return axios.get(`/getLeft`).then((res) => {
        //console.log("获取左转结果:");
       //  console.log(res.data);
        return res.data;
    });
};

//判断是否在右转状态
export const onRight = () => {
    return axios.get(`/getRight`).then((res) => {
        return res.data;
    });
};

//关门
export const closeDoor = () => {
    return axios.get(`/close`).then((res) => {
        return res.data;
    });
};


//称重
export const getWeight = () => {
    return axios.get(`/getWeight`).then((res) => {
        return res.data;
    });
};



//
// export const getFormula = () => {
//     return axios.get(`/getFormula`).then((res) => {
//         return res.data;
//     });
// };


//拍照
export const takePhoto = () => {
    return axios.get(`/takePhoto`).then((res) => {
        return res.data;
    });
};

//开始对新瓶子操作
export const startNewRecycling = ( code) => {
    return axios.get(`/startNewRecycling`, {params: {code: code}}).then((res) => {
        return res.data;
    });
};


//左翻
export const leftRotate = () => {

    return axios.get(`/leftRotate`).then((res) => {

        return res.data;
    });
};


//右翻
export const rightRotate = () => {
    return axios.get(`/rightRotate`).then((res) => {
        return res.data;
    });
};


//生成二维码
// export const createQRCode = (sn, code, type) => {
//     return axios.get(`/createQRCode`, {params: {sn: sn, code: code, type: type}}).then((res) => {
//         return res.data;
//     });
// };
//获取瓶子URL
export const getImage = (filepathname) => {
    //console.log(filepathname);
   return axios.get(`/getImage`, {params: {filepathname: filepathname}}).then((res) => {
        return res.data;
    });
};

//获取瓶子URL
export const delImage = (filepathname) => {
    console.log("删除文件" + filepathname);
   return axios.get(`/delImage`, {params: {filepathname: filepathname}}).then((res) => {
        return res.data;
    });
};

//异常的瓶子  ，保存瓶子信息到数据库
export const saveBottle = (url,machineCode,weight, code, type,picUrl) => {
    return Vue.prototype.$httpx.post(url +`api/PhyscialMachine/CreateRecycleItemSample`, {
            code: '',
            recylceItemCode:code,
            weight:weight,
            machineCode: machineCode,
            userTel: "",
            recyclableItemId: 0,
            picUrl: picUrl,
            result: type
        }).then((res) => {

        return res.data.response;
    }).catch((res) => {
        return 1;
    });

};



//上传错误代码
export const SetMachineState = (url,machineCode, status) => {
    return Vue.prototype.$httpx.post(url +`api/PhyscialMachine/SetMachineState`, {
            machineCode: machineCode,
            status:status,
            sdkVersion:0
        }).then((res) => {
         return res.data.response;
    });

};




//获取用户信息到数据库
export const getUserInfo = (url,tel) => {
    return Vue.prototype.$httpx.get(url +`api/PhyscialMachine/GetUserInfo`,{params: {tel: tel}}).then((res) => {
        return res.data.data;
    }).catch((res) => {
        return 1;
    });

};


//保存交易信息到远程服务器
export const saveTrade = (url,recyclableItemId,userId,tel,machineCode,operatorId,price,provinceCode,cityCode,areaCode) => {
    return axios.post(url+`api/PhyscialMachine/CreateOrder`,  {
        recyclableItemId:recyclableItemId,
        userId:userId,
        tel:tel,
        machineCode:machineCode,
        operatorId:operatorId,
        price:price,
        provinceCode:provinceCode,
        cityCode:cityCode,
        areaCode:areaCode
    }).then((res) => {
        return res.data;
    }).catch((res) => {
        return 1;
    });
};



